package com.jonniespratley.amfphpredux.utils
{
	import com.adobe.serialization.json.JSON;
	
	public class ServiceBrowserUtilities
	{
		public function ServiceBrowserUtilities()
		{
		}
		
		/**
		 * Cleans text for display 
		 * @param str string
		 * @return string Cleaned String
		 * 
		 */		
		public static function cleanText( str:String ):String
		{
		 return	str.split( '\\n' ).join( '\n' ).split( '\\"' ).join( '"' ).split( "\\'" ).join( "'" );
		}
		
		/**
		 * Format Bytes for output console 
		 * @param num
		 * @return string Formatted byte string
		 * 
		 */		
		public static function formatBytes( num:Number ):String
		{
			if( num < 1024 )
			{
				return Math.round( num ) + " bytes";
			}
			else if( num < 1024 * 1024 )
			{
				return Math.round( num / 1024 * 10 ) / 10 + " KB";
			}
			else
			{
				return Math.round( num / 1024 / 1024 * 10 ) / 10 + " MB";
			}
		}
		
		/**
		 * Dumps a object for debugging purposes 
		 * @param obj
		 * @return string String of dumped object
		 * 
		 */		
		public static function dumpObject( obj:Object ):String
		{
			var dumpString:String = "";
			
			//Dump out all values of an object
			for ( var s:String in obj )
			{
				dumpString += s + ":" + obj[s] + "\n";	
			}
			
			return dumpString;
		}
		
		public function getArgs( forWhat:Object ):Array
		{
			var parsedArgs:Array = new Array();
			
			if( forWhat is Array )
			{
				for( var i:int = 0; i < forWhat.length; i++ )
				{
					try
					{
						var text:String = forWhat[ i ].text;
						if( text == "" )
						{
							parsedArgs.push( null );
							continue;
						}
						if( text.indexOf( '{' ) == -1 && 
						    text.indexOf( '[' ) == -1 &&
						    text.indexOf( '"' ) == -1 &&
						    text.indexOf( "'" ) == -1 )
						{
							var val:Number = parseInt( text );
							if( isNaN( val ) )
							{
								text = ' " ' + text + ' " ';
							}
						}
						//push the decoded json to the args array
						parsedArgs.push( JSON.decode( text ) );
					}
					catch(e:*)
					{
						trace( "Argument #" + i + " is not a valid JSON string" );
						//return false;
					}
				}
			}
			return parsedArgs;
		}
		/* 
		public function getRequestStats( ):void
		{
			
			var d = testService.diagnostic;
			var profiling = testService.profiling;
			
			txt_info.text = "Total time: " + (getTimer() - time) + " ms\n";
			txt_info.text += "Encode time: " + d.encodeTime + " ms\n";
			txt_info.text += "Send/receive time: " + d.pingTime + " ms\n";
			txt_info.text += "Decode time: " + d.decodeTime + " ms\n";
			txt_info.text += "Network time: " + (d.pingTime - profiling.totalTime) + " ms\n\n";

			txt_info.text += "Total amfphp time: " + profiling.totalTime + " ms\n";
			txt_info.text += "Framework time: " + profiling.frameworkTime + " ms\n";
			txt_info.text += "Amf decode time: " + profiling.decodeTime + " ms\n";
			txt_info.text += "Service include time: " + profiling.includeTime + " ms\n";
			txt_info.text += "Service call time: " + profiling.callTime + " ms\n";
			txt_info.text += "Amf encode time: " + profiling.encodeTime + " ms\n\n";				
			
			var sbTime = getTimer();
			
			resultsText.text = ObjectUtil.toString(event.result);
			
			txt_trace.text = testService.traceMessages.join("\n");
			
			txt_info.text += "Sent data size: " + formatBytes(d.sentSize) + "\n";
			txt_info.text += "Received data size: " + formatBytes(d.receivedSize) + "\n";
			txt_info.text += "Average network speed: " + formatBytes(1000*(d.sentSize + d.receivedSize)/(d.pingTime - profiling.totalTime)) + "/s\n\n";

			txt_info.text += "Service browser processing time: " + (getTimer() - sbTime) + " ms\n";		
			time = getTimer();
		}
 */
	}
}